<?php


namespace app\index\controller;

use app\common\controller\Frontend;

class GetWechatAccessToken extends Frontend
{
    private $path = '';
    protected $noNeedLogin = ['*'];
    /**
     * 获取token
     */
    public function gettoken($is_refresh = false, $type = 'in') {
        //加载初始化配置
        $appid = $type == 'in' ? config('wechatwork')['appid'] : config('wechatwork')['appid_out'];
        $appsecret = $type == 'in' ? config('wechatwork')['appsecret'] : config('wechatwork')['appsecret_out'];
        $domain = config('wechatwork')['m_school'];
        $url = $is_refresh === false ? config('wechatwork')['m_shool_url'] : config('wechatwork')['refresh_url'];
        $m_shool_url = $domain . $url;
        //构建接口URL
        $url = $m_shool_url."?appid={$appid}&appsecret={$appsecret}";
        //请求接口(推荐使用curl)
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);
        $res = json_decode($data, true);
        if($res['e'] == 0) {
            $str = $res['d']['access_token']."\n".time();
            file_put_contents($this->path, $str);
            return $res['d']['access_token'];
        }else{
            echo $res['m'];exit;
        }
    }

    // 获取token
    public function get_token($type = 'in'){
        $path = 'token';
        $this->path = $type == 'in' ? $path . DS . 'wechat_token.txt' : $path . DS . 'wechat_token_out.txt';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        if(!file_exists($this->path)){
            // 请求接口获取token
            return $this->gettoken(false, $type);
        }
        $token_str = file_get_contents($this->path);
        if(!empty($token_str)){
            $token_arr = explode("\n", $token_str);
            if(time() - $token_arr[1] > config('wechatwork')['expires_in']){
                // 刷新token
                return $this->gettoken(true, $type);
            }
            if(is_array($token_arr)){
                return $token_arr[0];
            }else{
                return 'error!';
            }
        }
        return $this->gettoken(false, $type);
    }
    // 校内发送消息测试
    public function test(){
        list($status, $msg) = send_wechat_work_msg([200401061], '<a href="https://cgpt.qau.edu.cn/wap/teacher/projectlib/view/ids/662/tab_id/pending/wf_type/projectlib">采购管理平台计划详情http://qaucggl.qdzygx.com/wap/teacher/projectlib/view/ids/662/tab_id/pending/wf_type/projectlib</a> ');
//        list($status, $msg) = send_wechat_work_msg([18153227097,200401061,201401047], '<a href="https://cgpt.qau.edu.cn/wap/teacher/projectlib/view/ids/662/tab_id/pending/wf_type/projectlib">采购管理平台计划详情</a> ');
        echo $msg;exit;
    }

    public function logout(){
        list($status, $msg) = send_wechat_work_msg([18153227097], '<a href="https://cgpt.qau.edu.cn/wap/index/logout">采购管理平台退出登录</a> ');
//        list($status, $msg) = send_wechat_work_msg([18153227097,201401047], '<a href="https://cgpt.qau.edu.cn/wap/teacher/projectlib/view/ids/662/tab_id/pending/wf_type/projectlib">采购管理平台计划详情</a> ');
        echo $msg;exit;
    }
    // 重置token测试用
//    public function settime(){
//        $token_str = file_get_contents(CONF_PATH . 'token' . DS .'wechat_token.txt');
//        $token_arr = explode("\n", $token_str);
//        $token_arr[1] = $token_arr[1] - 10000;
//        file_put_contents(CONF_PATH . 'token' . DS .'wechat_token.txt', implode("\n", $token_arr));
//    }
    // 校外人员发送消息测试
   public function test1(){
       // list($status, $msg) = send_wechat_work_msg([18153227097], '测试消息 http://qaucggl.qdzygx.com/wap/review/projectlib/view/ids/650/tab_id/pending/wf_type/projectlib',);

        // list($status, $msg) = send_wechat_work_msg([18153227097], '您有一份校外人员贾小兵提交的入校预约申请需要审批。');
        // halt(send_wechat_work_msg([18153227097], '您有一份校外人员贾小兵提交的入校预约申请需要审批。'));
       // echo $msg;exit;
        // send_wechat_work_msg([201401047], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员 贾测试 提交的入校预约申请需要审批。</a>');
        list($status, $msg) = send_wechat_work_msg([201401047], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员 测试人员 提交的入校预约申请需要审批。</a>');
        echo '赵'.$msg;exit;

        // 201401047
        // list($status, $msg) = send_wechat_work_msg([200401061], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员 测试人员 提交的入校预约申请需要审批。</a>');
        list($status, $msg) = send_wechat_work_msg([18153227097], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员 测试人员 提交的入校预约申请需要审批。</a>');
        echo '杨'.$msg;exit;

   }
}